#!/usr/local/bin/php
<?php
require_once dirname(__FILE__) . '/lib/parser.php';
require_once 'Console/Getopt.php';

$short = 'vhc:t:';
$long  = array('verbose', 'help', 'categories=', 'target=');

/**
 * Print the help summary.
 */
function showHelp()
{
  $help = <<<END_TEXT
NAME
  writer - parse a Wikipedia xml dump, filter it by category and write it out as my mini wiki format

SYNOPSIS
  php writer.php [options] [wikipedia-dump-file(s)]

DESCRIPTION
  This is a helper to strip down the wikipedia data set so that you can 
  run indexing tests quicker.

OPTIONS
  -v, --verbose
    Print verbose logging messages
  -c FILE, --categories=FILE
    File containing category patterns, one per line.
  -t FILE, --target=FILE
    File to write out the stripped down output.
  -h, --help
    Show this help

EXAMPLE
  php writer.php --verbose --categories=/path/to/category.lst --target=/path/to/target.xml /path/to/wikipedia/dumps-split-*

END_TEXT;
  echo $help;
}

// set up the Console_Getopt argument parser
$con  = new Console_Getopt();
$args = $con->readPHPArgv();
array_shift($args);
$options = $con->getopt2($args, $short, $long);
$verbose = false;
$target  = null;
$files   = array();
$categories = array();

if ($options instanceof PEAR_Error) {
  echo "Pear Error: " . $options->getMessage() . "\n";
  die();
}

// parse the arguments
foreach ($options[0] as $option) {
  switch ($option[0]) {
  case '--verbose':
  case 'v':
    $verbose = true;
    break;
  case 'c':
  case '--categories':
    if (!file_exists($option[1]) || !is_readable($option[1])) {
      throw new Exception("Cannot read file [" . $option[1] . "]");
    }
    $categories = file($option[1], FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
    break;
  case 't':
  case '--target':
    $target = $option[1];
    break;
  case '--help':
  case 'h':
  default:
    showHelp();
    die();
    break;
  }
}

$files = $options[1];

// validate the all required arguments have been provided
if (!$files) {
  echo "ERROR: No files provided\n";
  die();
} elseif (!$target) {
  echo "ERROR: No target file provided.\n";
  die();
}

if ($verbose) {
  echo "Options parsed, setting up indexer\n";
}

// set up the parser actions
$action = new MyWikiXmlWriterAction($target);
if ($verbose) {
  $logger = new ConsoleLogger();
  $action = new ProgressLoggingAction($action, $logger, 'Indexed: ', 100);
}

$action = new CategoryPatternFilterAction($action, $categories);

if ($verbose) {
  $action = new ProgressLoggingAction($action, $logger, 'Parsed: ');
}

// create the parser
$parser = new WikipediaParser($action);
echo "Parser created\n";

if ($verbose) {
  $parser->setVerbose();
}

// parse the xml
try {
  $parser->parseFile($files);
} catch (Exception $e) {
  echo "Error: " . $e->getMessage() . "\n";
}
